package com.qk.controller;


import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import dto.BusinessDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /**
     * 新增商机
     *
     * @param business
     * @return
     */
    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("新增商机: {}", business);
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();

    }
//   列表查询商机
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("查询参数: {}", businessDto);
        PageResult<Business> pageResult = businessService.listBusinesses(businessDto);
        return Result.success( pageResult);
    }
//分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机: {}, {}", businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();


    }
    //查询回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询回显: {}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    @PutMapping
    public Result updateBusiness(@RequestBody Business business) {
        log.info("修改商机: {}", business);
        business.setUpdateTime(LocalDateTime.now());
        businessService.trackBusiness(business);
        return Result.success();
    }
    //踢回公海
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("踢回公海: {}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    //转客户
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("转客户: {}", id);
        businessService.toCustomer(id);
        return Result.success();


    }
    //公海池列表查询
    @GetMapping("/pool")
    public Result listPool(BusinessDto businessDto) {
        log.info("查询公海池列表: {}", businessDto);
        PageResult<Business> pageResult = businessService.poolList(businessDto);
        return Result.success(pageResult);
    }



}

